#!/bin/bash

# 检测指定的用户是否已经登陆系统

while true
do
    # 输入用户
    read -p "请输入检测的用户名: " user

    if [ $user == "quit" ] || [ $user == "exit" ] ; then
        exit
    fi

    # 获得当前已登录用户名列表
    users=`who | awk '{print $1}' | sort | uniq`

    # 枚举循环判定
    flag=0
    for u in $users
    do
        if [ $user == $u ]; then
            flag=1
            echo "该用户已登录"
        fi
    done

    if [ $flag -eq 0 ]; then
        echo "该用户不存在或未登录"
    fi

done